# set working directory #
data <- read.csv("third pilot - cognitive intelligence test - data.csv", header=TRUE, stringsAsFactors = FALSE)

library(lavaan)
library(effects)
library(psych)
library(multicon)

## coding for Raven test of cognitive intelligence #### 
data$raven1<- NA; data$raven1[data$Raven1==7] <- 1; data$raven1[data$Raven1!=7] <- 0
data$raven2<- NA; data$raven2[data$Raven2==4] <- 1; data$raven2[data$Raven2!=4] <- 0
data$raven3<- NA; data$raven3[data$Raven3==6] <- 1; data$raven3[data$Raven3!=6] <- 0
data$raven4<- NA; data$raven4[data$Raven4==2] <- 1; data$raven4[data$Raven4!=2] <- 0
data$raven5<- NA; data$raven5[data$Raven5==4] <- 1; data$raven5[data$Raven5!=4] <- 0
data$raven6<- NA; data$raven6[data$Raven6==7] <- 1; data$raven6[data$Raven6!=7] <- 0
data$raven7<- NA; data$raven7[data$Raven7==3] <- 1; data$raven7[data$Raven7!=3] <- 0
data$raven8<- NA; data$raven8[data$Raven8==8] <- 1; data$raven8[data$Raven8!=8] <- 0
data$raven9<- NA; data$raven9[data$Raven9==7] <- 1; data$raven9[data$Raven9!=7] <- 0
data$raven10<- NA; data$raven10[data$Raven10==3] <- 1; data$raven10[data$Raven10!=3] <- 0
data$raven11<- NA; data$raven11[data$Raven11==7] <- 1; data$raven11[data$Raven11!=7] <- 0
data$raven12<- NA; data$raven12[data$Raven12==5] <- 1; data$raven12[data$Raven12!=5] <- 0
data$raven13<- NA; data$raven13[data$Raven13==5] <- 1; data$raven13[data$Raven13!=5] <- 0
data$raven14<- NA; data$raven14[data$Raven14==4] <- 1; data$raven14[data$Raven14!=4] <- 0
data$raven15<- NA; data$raven15[data$Raven15==1] <- 1; data$raven15[data$Raven15!=1] <- 0

# count number of items attempted #
data$item1attempted <- NA; data$item1attempted[data$Raven1>0] <- 1;
data$item2attempted <- NA; data$item2attempted[data$Raven2>0] <- 1;
data$item3attempted <- NA; data$item3attempted[data$Raven3>0] <- 1;
data$item4attempted <- NA; data$item4attempted[data$Raven4>0] <- 1;
data$item5attempted <- NA; data$item5attempted[data$Raven5>0] <- 1;
data$item6attempted <- NA; data$item6attempted[data$Raven6>0] <- 1;
data$item7attempted <- NA; data$item7attempted[data$Raven7>0] <- 1;
data$item8attempted <- NA; data$item8attempted[data$Raven8>0] <- 1;
data$item9attempted <- NA; data$item9attempted[data$Raven9>0] <- 1;
data$item10attempted <- NA; data$item10attempted[data$Raven10>0] <- 1;
data$item11attempted <- NA; data$item11attempted[data$Raven11>0] <- 1;
data$item12attempted <- NA; data$item12attempted[data$Raven12>0] <- 1;
data$item13attempted <- NA; data$item13attempted[data$Raven13>0] <- 1;
data$item14attempted <- NA; data$item14attempted[data$Raven14>0] <- 1;
data$item15attempted <- NA; data$item15attempted[data$Raven15>0] <- 1;

with(data, describe(cbind(item1attempted,item2attempted,item3attempted,item4attempted,item5attempted,item6attempted,item7attempted,item8attempted,item9attempted,item10attempted,item11attempted,item12attempted,item13attempted,item14attempted,item15attempted)))

# create a table displaying how many items people attempted #
data$raventotalattempted<- with(data, (rowSums(cbind(item1attempted,item2attempted,item3attempted,item4attempted,item5attempted,item6attempted,item7attempted,item8attempted,item9attempted,item10attempted,item11attempted,item12attempted,item13attempted,item14attempted,item15attempted), na.rm=TRUE)))
table(data$raventotalattempted)

# calculate total raven total score and percent #
data$raventotal<- with(data, (rowSums(cbind(raven1,raven2,raven3,raven4,raven5,raven6,raven7,raven8,raven9,raven10,raven11,raven12,raven13,raven14,raven15), na.rm=TRUE)))
data$ravenpercent <- data$raventotal/15
with(data, describe(cbind(raventotal, ravenpercent,raventotalattempted)))

# calculate alpha for the scale #
with(data, alpha(data.frame(raven1,raven2,raven3,raven4,raven5,raven6,raven7,raven8,raven9,raven10,raven11,raven12,raven13, raven14, raven15), na.rm=TRUE))

# calculate split half reliability #
raven.rel <- with(data, data.frame(cbind(raven1,raven2,raven3,raven4,raven5,raven6,raven7,raven8,raven9,raven10,raven11,raven12,raven13, raven14, raven15)))
splithalf.r(raven.rel, sims = 1000, graph = TRUE)

# convert self rating from factor to NUMERIC #
data$ravenselfrating<- as.numeric(data$ravenselfrating)

# convert self rating to percentage #
data$ravenselfratingpercent <- data$ravenselfrating/15

# descriptives of self rating and ravens test #
with(data, describe(cbind(ravenselfrating,ravenselfratingpercent,raventotal,ravenpercent)))

# correlations of raven and selfrating #
with(data, corr.test(cbind(ravenselfrating,ravenselfratingpercent,raventotal,ravenpercent)))

# calculate the people who had accurate self-insight #
data$overestimater <- NA; data$overestimater[data$raventotal<data$ravenselfrating] <- 1; data$overestimater[data$raventotal>=data$ravenselfrating] <- 0
data$underestimater <- NA; data$underestimater[data$raventotal>data$ravenselfrating] <- 1; data$underestimater[data$raventotal<=data$ravenselfrating] <- 0
data$accurate <- NA; data$accurate[data$raventotal==data$ravenselfrating] <- 1; data$accurate[data$raventotal!=data$ravenselfrating] <- 0

# dislay the people who were over- or under- or correct estimaters #
xtabs(~ overestimater, data = data)
xtabs(~ underestimater, data = data)
xtabs(~ accurate, data = data)